﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhysicsSimulator
{
    class CBall : CGameObject
    {
        
        float _vx;

        public float Vx
        {
            get { return _vx; }
            set { _vx = value; }
        }
        float _vy;

        public float Vy
        {
            get { return _vy; }
            set { _vy = value; }
        }


        CVectorForce _vt;

        internal CVectorForce Vt
        {
            get { return _vt; }
            set { _vt = value; }
        }
        CVectorForce _vtx;

        internal CVectorForce Vtx
        {
            get { return _vtx; }
            set { _vtx = value; }
        }
        CVectorForce _vty;

        internal CVectorForce Vty
        {
            get { return _vty; }
            set { _vty = value; }
        }

        Texture2D _ballTexture;

        public Texture2D BallTexture
        {
            get { return _ballTexture; }
            set { _ballTexture = value; }
        }

        Vector2 _position;

        public Vector2 BallPositon
        {
            get { return _position; }
            set { _position = value; }
        }

        float _ballScale;

        public float BallScale
        {
            get { return _ballScale; }
            set { _ballScale = value; }
        }

        private bool allow_drawing = true;

        public bool AllowDrawing
        {
            get { return allow_drawing; }
            set { allow_drawing = value; }
        }

        Vector2 cam_position;

        public Vector2 CamPositon
        {
            get { return cam_position; }
            set { cam_position = value; }
        }

        public CBall(SpriteBatch spriteBatch, Vector2 position, float scale, float h, float l, float t, float vx, float vy) 
            : base(spriteBatch)
        {
            _position = position;
            _ballScale = scale;
            
            _vx = vx;
            _vy = vy;
            _vt = new CVectorForce(spriteBatch);
            _vtx = new CVectorForce(spriteBatch);
            _vty = new CVectorForce(spriteBatch);
        }

        public override void Init(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {
            _ballTexture = contentManager.Load<Texture2D>("Images/Launch/ball");

            _vt.Init(contentManager);
            _vt.Create(_position, Vector2.Zero, 2, Color.Red);

            _vtx.Init(contentManager);
            _vtx.Create(_position, Vector2.Zero, 2, Color.Blue);

            _vty.Init(contentManager);
            _vty.Create(_position, Vector2.Zero, 2, Color.Green);
            base.Init(contentManager);
        }

        public override void Update(GameTimerEventArgs timer)
        {

            _vt.Position = _position - (cam_position - new Vector2(240, 400));
            _vtx.Position = _position - (cam_position - new Vector2(240, 400));
            _vty.Position = _position - (cam_position - new Vector2(240, 400));

            _vtx.VectorForce = new Vector2(2 * _vx, 0);
            _vty.VectorForce = new Vector2(0, -2 * _vy);
            _vt.VectorForce = new Vector2(2 * _vx, -2 * _vy);
            base.Update(timer);
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            if (AllowDrawing)
            {
                _vtx.Draw(timer);
                _vty.Draw(timer);
                _vt.Draw(timer);
                SpriteBatch.Draw(_ballTexture, _position, null, Color.White, 0, new Vector2(_ballTexture.Width / 2, _ballTexture.Height / 2), _ballScale, SpriteEffects.None, 0);
            }
            base.Draw(timer);
        }
    }
}
